//
//  FoodClassVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/11/20.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "FoodClassVC.h"
#import "AddFoodClassVC.h"
#import "FoodClassCell.h"
#import "SortClassVC.h"
#import "AddFoodVC.h"


@interface FoodClassVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *_categoryArray;
}

@property(nonatomic,strong)UITableView *classTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (weak, nonatomic) IBOutlet UIButton *addClassBtn;
@property (weak, nonatomic) IBOutlet UIButton *sortBtnC;
@property(nonatomic,strong)NSMutableArray *foodClassArray;//数据源

@end

@implementation FoodClassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitle:@"管理分类" titleColor:[UIColor blackColor] font:font17];
  //  self.foodClassArray = @[@"菜品分类1",@"菜品分类2",@"菜品分类2",@"菜品分类3",@"菜品分类4",@"菜品分类5",@"菜品分类6",@"菜品分类7",@"菜品分类8"].mutableCopy;
    [self.view addSubview:self.classTableView];
    if(SCREEN_H==812) self.bottomConstraint.constant = 34;
    _addClassBtn.backgroundColor = MianRedColor;
    [_sortBtnC setTitleColor:MianRedColor forState:UIControlStateNormal];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getCategoryList];
}

#pragma  mark ========网络请求========
//获取分类列表
- (void)getCategoryList{
    __weak typeof (self)weakSelf = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/GoodsCate/categoryList" isactive:YES parameter:@{} successBlock:^(PPHttpRequest *request, NSArray* responseObj) {
        
        NSMutableArray *arr = @[].mutableCopy;
        [responseObj enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CategoryListModel *model = [CategoryListModel yy_modelWithJSON:obj];
            [arr addObject:model];
          
        }];
        weakSelf.foodClassArray = arr;
       
        [weakSelf.classTableView reloadData];
        
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        
    }];
}


#pragma  mark ========懒加载========
- (UITableView *)classTableView{
    if(_classTableView == nil){
        _classTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-50-NavigationHeight-TableSafe) style:UITableViewStylePlain];
        _classTableView.delegate = self;
        _classTableView.dataSource = self;
        _classTableView.estimatedSectionFooterHeight = 0;
        _classTableView.estimatedSectionHeaderHeight = 0;
        _classTableView.tableFooterView = [UIView new];
        _classTableView.tableHeaderView = [UIView new];
        [_classTableView registerClass:[FoodClassCell class] forCellReuseIdentifier:@"FoodClassCell"];
        
    }
    return _classTableView;
}

- (NSMutableArray *)foodClassArray{
    if(_foodClassArray == nil){
        _foodClassArray = @[].mutableCopy;
    }
    return _foodClassArray;
}
#pragma  mark ========排序 ，新增========
- (IBAction)addFoodClass:(UIButton*)sender {
    
    [self.navigationController pushViewController:[AddFoodClassVC new] animated:YES];
}

- (IBAction)SortClassAction:(id)sender {
    SortClassVC *vc = [SortClassVC new];
    vc.dataSort = self.foodClassArray;
   [self.navigationController pushViewController:vc animated:YES];
}


- (void)editFoodClass:(UIButton *)btn{
    AddFoodClassVC *vc = [AddFoodClassVC new];
    vc.dataModel = _foodClassArray[btn.tag];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.foodClassArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FoodClassCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoodClassCell" forIndexPath:indexPath];
    //cell.textLabel.text = self.foodClassArray[indexPath.row];
    cell.datamModel = _foodClassArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.editBtn.tag = indexPath.row;
    [cell.editBtn addTarget:self action:@selector(editFoodClass:) forControlEvents:UIControlEventTouchUpInside];
    __weak typeof (self)weakself = self;
    [cell.upClassBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        AddFoodVC *vc = [AddFoodVC new];
        vc.categoryArray = _foodClassArray;
        vc.category_id = cell.datamModel.categoryID;
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}
@end
